package com.victor.controller;

import com.victor.dao.ResumeDao;
import com.victor.pojo.Resume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private ResumeDao resumeDao;

    @RequestMapping("list")
    public String queryAll(HttpServletRequest request, HttpServletResponse response,
                           ModelMap modelMap) {
        List<Resume> resumes = resumeDao.findAll();
        modelMap.addAttribute("resumes", resumes);

        return "list";
    }

    @RequestMapping("add")
    public String add(HttpServletRequest request, HttpServletResponse response) {
        Resume resume = new Resume();
        resume.setName("new User");
        resume.setAddress("new address");
        resume.setPhone("12345678");
        resumeDao.save(resume);
        return "redirect:/resume/list";
    }

    @RequestMapping("update")
    public String update(HttpServletRequest request, HttpServletResponse response, Long id) {

        Resume resume = new Resume();
        resume.setId(id);
        resume.setName("update user");
        resume.setAddress("update address");
        resume.setPhone("99999999");
        resumeDao.save(resume);
        return "redirect:/resume/list";
    }

    @RequestMapping("delete")
    public String delete(HttpServletRequest request, HttpServletResponse response, Long id) {
        Resume resume = new Resume();
        resume.setId(id);
        resumeDao.delete(resume);
        return "redirect:/resume/list";
    }
}
